package com.bjsxt.common.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.springframework.stereotype.Component;

@Component("fileCopyUtil")
public class FileCopyUtil
{
	public boolean copyFile(InputStream is,OutputStream os)
	{
		BufferedInputStream bis = new BufferedInputStream(is);
		BufferedOutputStream bos = new BufferedOutputStream(os);
		int len = 0;
		byte[] flush = new byte[1024*3];
		try
		{
			while(-1!=(len=(bis.read(flush))))
			{
				bos.write(flush, 0, len);
				bos.flush();
			}
			return true;
		} catch (IOException e)
		{
			e.printStackTrace();
		}finally
		{
			try
			{
				bos.close();
				bos = null;
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			try
			{
				bis.close();
				bos = null;
			} catch (IOException e)
			{
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public boolean copyFile(File souFile,File destFile)
	{
		try
		{
			copyFile(new BufferedInputStream(new FileInputStream(souFile)),new BufferedOutputStream(new FileOutputStream(destFile)));
			return true;
		} catch (FileNotFoundException e)
		{
		}
		return false;
	}
	public static void main(String[] args)
	{
		File old = new File("C:/Users/Administrator/Desktop/loan/WebContent/WEB-INF/lib/commons-io-2.4.jar");
		
		new FileCopyUtil().copyFile(old, new File("d:/a.jar"));
		
	}
}
